package com.supor.aiot.food.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.supor.aiot.food.R;
import com.supor.aiot.food.bean.BindDeviceBean;

import java.util.List;


public class SelectDeviceAdapter extends RecyclerView.Adapter<SelectDeviceAdapter.RecipeDeviceHolder> {
    private final List<BindDeviceBean> mList;
    private final Context mContext;
    private final onRecipeDeviceClick recipeDeviceClick;
    private int selectedPosition = -1;

    public SelectDeviceAdapter(List<BindDeviceBean> mList, Context mContext, onRecipeDeviceClick listener) {
        this.mList = mList;
        this.mContext = mContext;
        this.recipeDeviceClick = listener;
    }

    @NonNull
    @Override
    public SelectDeviceAdapter.RecipeDeviceHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_select_device, parent, false);
        return new RecipeDeviceHolder(view);
    }

    public void setPosition(int position) {
        this.selectedPosition = position;
        Log.e("nail", "selectedPosition = " + position + "mlist.size =" + mList.size());
        for (int i = 0; i < mList.size(); i++) {
            if (i == position) {
                mList.get(position).setSelected(true);
            } else {
                mList.get(i).setSelected(false);
            }
        }
        notifyDataSetChanged();
    }

    @Override
    public void onBindViewHolder(@NonNull RecipeDeviceHolder holder, int position) {
        BindDeviceBean deviceBean = mList.get(position);
        holder.llDeviceOuter.setOnClickListener(view -> recipeDeviceClick.OnDeviceClick(position, deviceBean.isSelected()));

        if (!TextUtils.isEmpty(deviceBean.getModelTypeName()))
            holder.tvDeviceDesc.setText(deviceBean.getModelTypeName());

        if (deviceBean.isBind()) {
            holder.ivMine.setVisibility(View.VISIBLE);
        } else {
            holder.ivMine.setVisibility(View.GONE);
        }

        if (position == selectedPosition) {
            if (deviceBean.isSelected()) {
                holder.tvDeviceDesc.setTextColor(mContext.getResources().getColor(R.color.normal_btn_normal));
            } else {
                holder.tvDeviceDesc.setTextColor(mContext.getResources().getColor(R.color.content_default));
            }
        } else {
            holder.tvDeviceDesc.setTextColor(mContext.getResources().getColor(R.color.content_default));
        }
    }


    @Override
    public int getItemCount() {
        return mList.size();
    }

    public static class RecipeDeviceHolder extends RecyclerView.ViewHolder {
        public LinearLayout llDeviceOuter;
        public TextView tvDeviceDesc;
        public ImageView ivMine;

        public RecipeDeviceHolder(@NonNull View itemView) {
            super(itemView);
            llDeviceOuter = itemView.findViewById(R.id.rl_device_outer);
            tvDeviceDesc = itemView.findViewById(R.id.tv_device_type);
            ivMine = itemView.findViewById(R.id.iv_my_device);
        }
    }

    public interface onRecipeDeviceClick {
        void OnDeviceClick(int position, boolean state);
    }

}
